#include <bits/stdc++.h>
// 2025/02/07
// tag: 
// Author: Zhang Muen
using namespace std;

using int64 = int64_t;

int n, m, q, t[201], f[201][201];

void add_edge(int pos){
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            f[i][j] = f[j][i] = min(f[i][j], f[i][pos] + f[j][pos]);
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> t[i];
    memset(f, 0x3f, sizeof(f));
    for (int i = 1, u, v, w; i <= m; i++)
        cin >> u >> v >> w, f[u + 1][v + 1] = f[v + 1][u + 1] = w;
    for (int i = 1; i <= n; i++)
        f[i][i] = 0;
    cin >> q;
    int p = 1;
    for (int i = 1, ti, s, e; i <= q; i++) {
        cin >> s >> e >> ti;
        while (t[p] <= ti && p <= n)
            add_edge(p++);
        if (f[s + 1][e + 1] == 0x3f3f3f3f || t[s + 1] > ti || t[e + 1] > ti)
            cout << -1 << endl;
        else
            cout << f[s + 1][e + 1] << endl;
    }
    return 0;
}